package com.syl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syl.dao.CommentMapper;
import com.syl.dao.StatisticMapper;
import com.syl.model.domain.Comment;
import com.syl.model.domain.Statistic;
import com.syl.service.ICommentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CommentServiceImpl implements ICommentService {

  @Autowired
  private CommentMapper commentMapper;
  @Autowired
  private StatisticMapper statisticMapper;

  // 根据博文id分页查询评论
  @Override
  public PageInfo<Comment> getComments(Integer aid, int page, int count) {
    PageHelper.startPage(page, count);
    List<Comment> commentList = commentMapper.selectCommentWithPage(aid);
    PageInfo<Comment> commentInfo = new PageInfo<>(commentList);
    return commentInfo;
  }

  // 用户发表评论
  @Override
  public void pushComment(Comment comment) {
    commentMapper.pushComment(comment);
    // 更新博文评论数据量
    Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
    statistic.setCommentsNum(statistic.getCommentsNum() + 1);
    statisticMapper.updateArticleCommentsWithId(statistic);
  }

}

